
//
//  LSYiBanVC.m
//  LaSaCivilAffairsDepartment
//
//  Created by 姜晓延 on 2019/8/22.
//  Copyright © 2019 wpkj. All rights reserved.
//

#import "LSYiBanVC.h"
#import "JSONUtils.h"
#import "NiWenListCell.h"
#import "YiBanInfoVC.h"
#define SelectViewWidth (147/3*2)
#define SelectViewHeight 100
//(275/3*2)
#define headerSearchViewHeight 103
#define CountPerPage 10
@interface LSYiBanVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray <NSDictionary *> *selectButtonsArray;

@property (nonatomic, strong) UITableView *yiBannTableView;
@property (nonatomic, strong) NSMutableArray *dataArrayM;

@property (nonatomic, strong) UITextField *yiBannName;

@property (nonatomic, strong) UITextField *yiBannNum;

@property (nonatomic, assign) NSInteger tablePage;


@end

static NSString *yiBannTableViewCellID = @"yiBannTableViewCellID";
@implementation LSYiBanVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"已办事项";
    [self setUpHeaderSearchView];
    [self.view addSubview:self.yiBannTableView];
    WS
    self.yiBannTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.tablePage = 1;
        [weakSelf loadData];
    }];
    self.yiBannTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.tablePage++;
        [weakSelf loadData];
    }];
    [self.yiBannTableView.mj_header beginRefreshing];
}
- (void)setUpHeaderSearchView {
    UIView *headerSearchView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, headerSearchViewHeight)];
    headerSearchView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:headerSearchView];
    UIView *headerViewBottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, headerSearchViewHeight-0.5, ScreenWidth, 0.5)];
    headerViewBottomLineView.backgroundColor = hexColor(CCCCCC);
    [headerSearchView addSubview:headerViewBottomLineView];
    
    // 拟文名
    UITextField *nameTF = [[UITextField alloc] initWithFrame:CGRectMake(15, 5+4, (ScreenWidth-30), 36)];
    self.yiBannName = nameTF;
    nameTF.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:12];
    nameTF.layer.borderColor = hexColor(dbdbdb).CGColor;
    nameTF.layer.borderWidth = 1;
    nameTF.returnKeyType = UIReturnKeySearch;
    nameTF.placeholder = @"请输入流程名称";
    nameTF.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 1)];
    nameTF.leftViewMode = UITextFieldViewModeAlways;
    [headerSearchView addSubview:nameTF];
    
    
    UITextField *cardNumberTF = [[UITextField alloc] initWithFrame:CGRectMake(15, 54+4, ScreenWidth-30-55-15, 36)];
    self.yiBannNum = cardNumberTF;
    cardNumberTF.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:12];
    cardNumberTF.layer.borderColor = hexColor(dbdbdb).CGColor;
    cardNumberTF.layer.borderWidth = 1;
    cardNumberTF.returnKeyType = UIReturnKeySearch;
    cardNumberTF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    cardNumberTF.placeholder = @"请输入流程编号";
    cardNumberTF.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 1)];
    cardNumberTF.leftViewMode = UITextFieldViewModeAlways;
    [headerSearchView addSubview:cardNumberTF];
    
    UIButton *searchButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-15-55, 54+4, 55, 36)];
    searchButton.layer.cornerRadius = 4;
    searchButton.layer.masksToBounds = true;
    searchButton.backgroundColor = NAV_COLOR;
    [searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    searchButton.titleLabel.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:12];
    [searchButton addTarget:self action:@selector(loadData) forControlEvents:UIControlEventTouchUpInside];
    [headerSearchView addSubview:searchButton];
}
- (void)loadData {
    [self.view endEditing:YES];
    NSDictionary *param = @{@"procDefName":self.yiBannName.text,@"procDefId":self.yiBannNum.text,@"rows":[NSString stringWithFormat:@"%d",CountPerPage],@"page":[NSString stringWithFormat:@"%ld",(long)self.tablePage]};
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:true];
    [NetworkUtils requestNOHandleWithMethod:@"POST" URL:[NSString stringWithFormat:@"%@&token=%@",wdyb,[UserInfoModel sharedInstance].token] param:param completionHandler:^(id responseObject, NSError *connectionError) {
        NSLog(@"参数：%@",param);
        [hud hideAnimated:true];
        if (!connectionError) {
            NSNumber *status = [responseObject objectForKey:@"status"];
            if (self.tablePage == 1) {
                [self.yiBannTableView.mj_header endRefreshing];
                [self.dataArrayM removeAllObjects];
            }
            if (status.integerValue == 200) {
                NSArray *dataArray = [responseObject objectForKey:@"data"];
                [self.dataArrayM addObjectsFromArray:dataArray];
                if (self.dataArrayM.count < self.tablePage*CountPerPage) {
                    [self.yiBannTableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.yiBannTableView.mj_footer endRefreshing];
                }
                [self.yiBannTableView reloadData];
            } else {
                [self.yiBannTableView.mj_footer endRefreshingWithNoMoreData];
                [MBProgressHUD showTipMessageInWindow:responseObject[@"message"]];
            }
        } else {
            if (self.tablePage == 1) {
                [self.yiBannTableView.mj_header endRefreshing];
            }else{
                [self.yiBannTableView.mj_footer endRefreshingWithNoMoreData];
            }
            [MBProgressHUD showTipMessageInWindow:connectionError.localizedDescription];
        }
    }];
}








#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArrayM.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NiWenListCell *cell = [tableView dequeueReusableCellWithIdentifier:yiBannTableViewCellID];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"NiWenListCell" owner:nil options:nil]lastObject];
    }
    [cell configYiBanDict:self.dataArrayM[indexPath.row]];
    
    cell.xuanzhongbtnclicked = ^(BOOL selected) {
        
    };
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YiBanInfoVC *yvc = [[YiBanInfoVC alloc]init];
    yvc.dict = self.dataArrayM[indexPath.row];
    [self.navigationController pushViewController:yvc animated:YES];
}
- (UITableView *)yiBannTableView {
    if (!_yiBannTableView) {
        _yiBannTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, headerSearchViewHeight, ScreenWidth, ScreenHeight-SafeAreaTopHeight-headerSearchViewHeight)];
        _yiBannTableView.delegate = self;
        _yiBannTableView.dataSource = self;
        _yiBannTableView.backgroundColor = hexColor(F0F0F0);
        _yiBannTableView.tableFooterView = [UIView new];
        _yiBannTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _yiBannTableView;
}

- (NSArray<NSDictionary *> *)selectButtonsArray {
    if (!_selectButtonsArray) {
        _selectButtonsArray = @[@{@"title":@"  新 增",@"image":@"xinzen"},
                                @{@"title":@"  编 辑",@"image":@"banli"},
                                @{@"title":@"  催 办",@"image":@"cuiban"},
                                @{@"title":@"  督 办",@"image":@"duban"},
                                @{@"title":@"  归 档",@"image":@"guidang"},
                                @{@"title":@"  流程图",@"image":@"lct"}];
    }
    return _selectButtonsArray;
}

- (NSMutableArray *)dataArrayM {
    if (!_dataArrayM) {
        _dataArrayM = [[NSMutableArray alloc] init];
    }
    return _dataArrayM;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
